# -*- coding:utf-8 -*-
"""
@file       zuobiao.py
"""
from sympy import cos, sin, acos
from sympy.abc import rho, theta

from mathsolver.functions.base import *


class JiConvertZhiPoint(BaseFunction):  # 从极坐标转化为直角坐标
    def solver(self, *args):  # in: BasePoint, out: BasePoint
        self.label.add("将极坐标转化为直角坐标")
        j_pt = args[0].sympify()
        j_rho, j_th = j_pt
        self.steps.append(["", "令x = %s, y = %s" % (new_latex(rho * cos(theta)), new_latex(rho * sin(theta)))])
        pt_x = simplify(j_rho * cos(j_th))
        pt_y = simplify(j_rho * sin(j_th))
        pt = BasePoint({"name": "", "value": [pt_x, pt_y]})
        self.steps.append(["", "∴转化为直角坐标为：%s" % pt.printing()])
        self.output.append(pt)
        return self


class ZhiConvertJiPoint(BaseFunction):  # 从直角坐标转化为极坐标
    def solver(self, *args):  # in: BasePoint, out: BasePoint
        self.label.add("将极坐标转化为直角坐标")
        z_pt = args[0].sympify()
        z_x, z_y = z_pt
        rho_num = sqrt(z_x ** 2 + z_y ** 2)
        self.steps.append(["", "由题意得：%s = \\sqrt{%s^2+%s^2}" % (new_latex(rho), new_latex(z_x), new_latex(z_y))])
        self.steps.append(["", "令%s = %s, %s = %s" % (new_latex(z_x), new_latex(rho * cos(theta)), new_latex(z_y), new_latex(rho * sin(theta)))])
        c_rs = acos(z_x / rho_num)
        if z_y < 0:
            c_rs = 2 * pi - c_rs
        pt = BasePoint({"name": "", "value": [rho_num, c_rs]})
        self.steps.append(["", "∴%s转化为直角坐标为：%s" % (args[0].printing(), pt.printing())])
        self.output.append(pt)
        return self


class ZhiConvertJiPoints(BaseFunction):
    def solver(self, *args):
        for item in args[0].sympify():
            self.append(ZhiConvertJiPoint().solver(BasePoint(item)))
        return self


if __name__ == '__main__':
    pass
